1408F - Two Different - CodeForces Solution


constructive algorithms divide and conquer *2300

Please click on ads to support us..

Python Code:

def pairss(n):
	res = [];i = 1
	while i < n:
		for k in range(0,n,i*2):
			for j in range(i):res.append((j+k,j+i+k))
		i *= 2
	return res
n = int(input());z = 1
while z * 2 < n:z *= 2
q = pairss(z);res = []
for a, b in q:res.append((a+1, b+1))
for a, b in q:res.append((n-a, n-b))
print(len(res))
for a, b in res:print(a,b)

C++ Code:

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef pair<int,int>pii;
const int maxn=1e5+10;
vector<pii>ans;
int n,ind,K;
inline void match(int l,int r)
{
    if(l==r)return;int mid=(l+r)>>1;
    match(l,mid),match(mid+1,r);
	for(int i=l;i<=mid;i++) ans.pb({i,i-l+mid+1});
}
inline void solve()
{
    cin>>n,ind=log2(n),K=(1<<ind);
    match(1,K),match(n-K+1,n);
    cout<<ans.size()<<'\n';
    for(auto [l,r]:ans) cout<<l<<" "<<r<<'\n';
}
signed main(){ios::sync_with_stdio(false);cin.tie(NULL);return solve(),0;}


Comments

Submit
0 Comments
More Questions

1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways